class Tree:
    def __init__(self):
        self.val = 0
        self.left = self.right = None


class CountIntervals(object):

    def __init__(self):
        self.min_val = 1
        self.max_val = 10 ** 9
        self.root = Tree()

    def back(self, node):
        if not node.left:
            node.left = Tree()
            node.right = Tree()

    def update(self, node, ls, rs, l, r):
        if ls >= l and rs <= r:
            node.val = rs - ls + 1
            return node.val
        else:
            if node.val == rs - ls + 1:
                return node.val
            mid = (ls + rs) // 2
            self.back(node)
            if mid >= l:
                self.update(node.left, ls, mid, l, r)
            if mid + 1 <= r:
                self.update(node.right, mid + 1, rs, l, r)
            node.val = node.left.val + node.right.val
            return node.val

    def add(self, left, right):
        """
        :type left: int
        :type right: int
        :rtype: None
        """
        self.update(self.root, self.min_val, self.max_val, left, right)

    def count(self):
        """
        :rtype: int
        """
        return self.root.val
